//
//  WBEmoticonToolBar.swift
//  0829-Weibo
//
//  Created by 毕仲阳 on 16/9/9.
//  Copyright © 2016年 mycompany. All rights reserved.
//

import UIKit
enum WBEmoticonType: Int{
    case RECENT = 0, DEFAULT, EMOJI, LXH
}
class WBEmoticonToolBar: UIView {

    var currentSelectedButton:UIButton?
    var emoticonTypeChangeClosure:((WBEmoticonType)->())?
    
    var selectedIndex:IndexPath?{
        didSet{
            let button = self.stackView.viewWithTag(selectedIndex!.section)! as! UIButton
            if currentSelectedButton == button {
                return
            }
            currentSelectedButton?.isSelected = false
            button.isSelected = true
            currentSelectedButton = button
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI()
    {
        self.addSubview(stackView)
        stackView.snp_makeConstraints { (make)->Void in
            make.edges.equalTo(self)
        }
        addChildButton(title: "最近", bgImgNamed: "compose_emotion_table_left", type: .RECENT)
        addChildButton(title: "默认", bgImgNamed: "compose_emotion_table_mid", type: .DEFAULT)
        addChildButton(title: "Emoji", bgImgNamed: "compose_emotion_table_mid", type: .EMOJI)
        addChildButton(title: "浪小花", bgImgNamed: "compose_emotion_table_right", type: .LXH)

    }
    
    private func addChildButton(title: String, bgImgNamed: String,type:WBEmoticonType) {
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(childButtonClick(button:)), for: UIControlEvents.touchUpInside)
        button.setTitle(title, for: UIControlState.normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        // 设置不同状态下的文字颜色
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.setTitleColor(UIColor.gray, for: UIControlState.selected)
        
        // 设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_normal"), for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named: "\(bgImgNamed)_selected"), for: UIControlState.selected)
        
        stackView.addArrangedSubview(button)
    }
    
    //MARK: -按钮点击事件
    @objc private func childButtonClick(button:UIButton){
        if currentSelectedButton == button {
            return
        }
        currentSelectedButton?.isSelected = false
        button.isSelected = true
        
        currentSelectedButton = button
        
        emoticonTypeChangeClosure?(WBEmoticonType(rawValue: button.tag)! )
    }
    // MARK: - 懒加载事件
    
    private lazy var stackView: UIStackView = {
        // FDStackView
        let v = UIStackView()
        v.tag = 1000
        // 设置布局方向
        // v.axis =
        v.distribution = .fillEqually
        return v
    }()
    

}
